// pages/login/administrator-exprssorder/exprss-order/exprss-order.js
const db=wx.cloud.database()
import common from "../../../../utils/public.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    expressId:"",
    datelist:[],
    user:[],
    image:false,
    sure1:false,
    name:"",
    caballero:""
  },

  // 判断该快件是否被取走
  txtPickup(id){
    var that=this
    db.collection('express').doc(id).get().then(res=>{
      var caballero=res.data.caballero
      if(caballero.length==0){
        that.setData({
          sure1:true
        })
      }else{
        that.setData({
          sure1:false
        })
        that.getcaballero(res.data.caballero)
      }
    })
  },

  //获取骑手的信息
  getcaballero(userid){
    var that=this
    db.collection('user').where({
      openid:userid
    }).get().then(res=>{
      that.setData({
        name:res.data[0].realname,
        caballero:res.data[0].caballero
      })
      console.log(res.data[0].caballero)      
    })
  },
  

  //获取快递信息
  gettext(expressId){
    var that=this
    db.collection('express').where({
      _id:expressId
    }).get().then(res=>{
      var newlist=res.data[0]
      newlist.time=common.getMyData(newlist.time,"Y-m-d H:i:s")
      newlist.image="cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/"+newlist.image
      that.setData({
        datelist:newlist,
      })      
    })

    db.collection('user').where({
      _openid:that.data.datelist._openid
    }).get().then(res=>{
      that.setData({
        user:res.data[0]
      })
    })
  },

  //点击确认取件事件
  sure(){
    wx.showLoading({
      title: '数据加载中...',
      mask:true
    })
    var that=this
    var expressid=that.data.expressId
    var text="配送中"

    wx.cloud.callFunction({
      name:'login',
      success(res){
        var caballero=res.result.openid
        that.updateexpres(expressid,text,caballero)
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '确认成功',
              icon: 'success',
              duration: 2000
            })
            that.txtPickup(that.data.expressId)
            that.gettext(that.data.expressId)
            that.getcaballeroTime(that.data.expressId)
            // wx.navigateBack({
            //   delta: 1,
            // })
            that.back()
          },
        })
      }
    })
  },

  // 返回上一页
  back: function () {
    let pages = getCurrentPages(); // 获取加载的页面对象
    let prevPage = pages[pages.length - 2]; // 获取上一页的页面对象，1代表当前页，2代表上一页
    // 把上一页参数num值改为1
    prevPage.setData({
      reder: true
    })
    // 返回上一页
    wx.navigateBack({
        delta: 1
    })
},


//点击取件按钮、获取取件骑手取件时间
getcaballeroTime(expressid){
  wx.cloud.callFunction({
    name:'express-caballero_timeUpdate',
    data:{
      id:expressid,
      caballero_time:Date.parse(new Date())/1000
    }
  })
},

  

  //更改快递信息
  updateexpres(expressid,text="配送中",caballero){
    wx.cloud.callFunction({
      name:'express-caballleroUpdate',
      data:{
        id:expressid,
        text:text,
        caballero:caballero
      },
      success(res){
        console.log(res)
      }
    })
  },

  imagess(){
    this.setData({
      image:true,
    })
  },

  image(){
    this.setData({
      image:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '数据加载中...',
    })
    var that=this
    that.setData({
      expressId:options.expressId
    })
    that.txtPickup(options.expressId)
    that.gettext(options.expressId)
    wx.hideLoading()
  },

  //点击了无法取件
  nonsure(){
    var that=this
    wx.showModal({
      title: '提示',
      content: '是否确定该快件无法取件',
      success (res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name:'login',
            success(resx){
              let caballero=resx.result.openid
              let text="出错"
              let expressid=that.data.expressId
              that.updateexpres(expressid,text,caballero)
              wx.showLoading({
                title: '数据提交中...',
                mask:true
              })
              db.collection('nonepickup').add({
                 data:{
                 expressId:that.data.expressId,
                 time:Date.parse(new Date())/1000
                }
              }).then(res=>{
               console.log(res)
               wx.hideLoading({
                success: (res) => {
                  that.txtPickup(that.data.expressId)
                  that.gettext(that.data.expressId)
                that.back()
                wx.showToast({
                  title: '提交成功',
                  icon:'success',
                  duration:2000
                })
              },
            })
          })

          }
          })


          

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})